<script setup>
import { reactive, ref } from 'vue';
import router from '@/router';
import loginApi from '@/api/login_api.js';
// import { adminStore, tokenStore } from '../store/store.js';
const activeIndex = ref('1')

// 取出当前登录用户的信息
const loginAdmin = ref({
  name: "已登录",
 });
getLogin();

// 菜单  用户管理， 分类管理， 商品管理
const menuData = ref([
  {name: '寄养管理', icon: 'Sunrise', children: [
      {name: '寄养宠物查询', icon: 'GobletSquareFull', path: "/foster"},
      {name: '寄养预约', icon: 'TrendCharts', path: "/fosterBooking"},
    ]},
  {name: '领养管理', icon: 'Sunny', children: [
      {name: '领养宠物查询', icon: 'Document', path: "/adopt"},
      {name: '领养预约', icon: 'Calendar', path: "/adoptBooking"},
    ]},
  {name: '会员管理', icon: 'User', children: [
      {name: '会员列表', icon: 'UserFilled', path: "/user"},
    ]},
  {name: '宠物管理', icon: 'Avatar', children: [
      {name: '宠物列表', icon: 'List', path: "/pet"},
    ]},
  {name: '管理员管理', icon: 'Lock', children: [
      {name: '管理员列表', icon: 'Setting', path: "/manager"},
    ]},
  {name: '数据显示', icon: 'Lock', children: [
      {name: '表格展示', icon: 'Setting', path: "/table"},
    ]},

]);

async function getLogin(){
  let token=localStorage.getItem("token");//获取token
  if(!token){// 没有token
    // 跳转到登录
    router.push("/login")
    }
  const res=await loginApi.logininfo();//获取当前登录用户信息
  loginAdmin.value=res;//赋值给当前登录用户
  // adminStore().update(res);
}
// 用户下拉菜单的点击
function handleSelect(key, keyPath){
  if (key == 'logout'){
    // 用户退出
    logout();
  }
}
// 左侧菜单动作
function handleMenu(key){
  if (key){
    router.push(key);
  }
}
// 退出
function logout(){
  // 清除Token
  localStorage.removeItem("token");//清除token
  // 跳转到登录
  router.push('/')//退出之后的跳转页面
}

// 左侧菜单展开折叠
const isCollapse = ref(false);
</script>

<template>
  <div class="common-layout">

    <el-container>
      <el-header>
        <el-menu :default-active="activeIndex"
                 class="el-menu-demo"
                 mode="horizontal"
                 :ellipsis="false"
                 background-color=black
                 text-color=pink
                 active-text-color=lightskyblue
                 @select="handleSelect">

          <el-menu-item index="0">
            <img style="height: 80%" src="../assets/logo.png" alt="抱抱我" />
            <span style="padding-left: 50px; font-size: 24px; color:pink;">抱抱窝——宠物领养管理系统</span>
          </el-menu-item>
          <div class="flex-grow" />
          <el-sub-menu index="1">
            <template #title>
              <el-icon><Stamp></Stamp></el-icon>
              {{ loginAdmin?loginAdmin.manager_name:'没登录' }}
            </template>
            <el-menu-item index="1-1">修改信息</el-menu-item>
            <el-menu-item index="1-2">重置密码</el-menu-item>
            <el-menu-item index="logout">退出
              <el-icon><SwitchButton></SwitchButton></el-icon>
            </el-menu-item>
          </el-sub-menu>

        </el-menu>
      </el-header>
      <el-container>
        <el-aside :width="1">
          <div style="height: 60px;line-height: 60px; text-align: right; color:black; font-size: 20px; margin-right: 24px;">
            <el-icon @click="isCollapse=!isCollapse">
              <Expand v-if="isCollapse"></Expand>
              <Fold v-else></Fold>
            </el-icon>
          </div>
          <el-menu active-text-color="#ffd04b" background-color=black class="el-menu-vertical-demo"
                   default-active="/charts" text-color=pink :collapse="isCollapse"
                   @select="handleMenu">
            <!-- 动态生成菜单 -->
            <el-sub-menu v-for="(menu, index) in menuData" :index="index.toString()">
              <template #title>
                <component
                    class="icons"
                    :is="menu.icon"
                    style="width: 1em; height: 1em; margin-right: 8px" >
                </component>
                <span>{{ menu.name }}</span>
              </template>
              <el-menu-item v-for="(child, ind) in menu.children" :index="child.path">
                <el-icon><component
                    :is="child.icon">
                </component></el-icon>
                <span>{{ child.name }}</span>
              </el-menu-item>
            </el-sub-menu>

          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>



<style>
.flex-grow {
  flex-grow: 1;
}

.common-layout,
.el-container {
  height: 100%;
}

.el-header {
  --el-header-padding: 0;
  --el-header-height: 80px;
  box-sizing: border-box;
  flex-shrink: 0;
  height: var(--el-header-height);
  padding: var(--el-header-padding);
}

.el-menu--horizontal {
  height: 80px;
}

.el-aside {
  background-color: black;
  /* 新增：让 aside 占满父容器（el-container）的高度 */
  height: calc(100vh - 80px);
}

.el-main{
  padding: 0px;
}

.el-menu{
  border-width: 0px;
}
.el-menu-item{
  font-size: 16px;
}
.el-menu-demo{
  box-shadow: 0 2px 4px rgba(0,0,0,0.25);
}

.el-sub-menu__title{
  font-size: 16px;
}

</style>